package com.csu.ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.csu.bean.Member;
import com.csu.dao.MemberDao;
import com.csu.dao.MemberDaoImpl;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

public class TablePanel extends JPanel {

	private JTable table;				//表格
	private String[] columnNames;		//表头
	private Object[][] rowData;  		//表格数据
	private DefaultTableModel model;	//数据模型
	private int currPage = 1;				//当前页面
	private int pageSize = 10;			//每页显示条数
	private int totalPage;				//页面总数
	private JButton prevBtn;			//上一页
	private JButton nextBtn;			//下一页
	JPanel panel = new JPanel();		//按钮panel
	private JLabel label1 = new JLabel();
	private JLabel label2 = new JLabel();
	private JLabel label3 = new JLabel();
	private MemberDao memberDao = new MemberDaoImpl();
	
	public void setLabel2() {
		label2.setText(currPage+"");
	}

	public void setLabel3() {
		label3.setText(" / "+totalPage+" 页");
	}
	
	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}
	
	public TablePanel(){
		//设置边界布局
		setLayout(new BorderLayout());
	}
	
	//初始化数据
	public void initData() {
		columnNames = new String[]{"用户类型","用户名"};
		//获取第一页的成员信息，并转为二维数据
		List<Member> memberList = memberDao.getMemberByPage(0, pageSize);
		rowData = getRowData(memberList);
		//总页数
		totalPage = (int)Math.ceil(memberDao.getMemberCount()/(1.0*pageSize));
		model = new DefaultTableModel(rowData, columnNames);
		table = new JTable();
		table.setModel(model);
		//设置表格内容
		table.setFont(new Font(null,Font.BOLD,14));
		table.setRowHeight(30);
		//添加表头
		add(table.getTableHeader(),BorderLayout.NORTH);
		//添加表格内容
		add(table,BorderLayout.CENTER);
	}
	
	//根据用户名初始化数据
	public void initData(String acct) {
		columnNames = new String[]{"用户类型","用户名"};
		//获取第一页的成员信息，并转为二维数据
		List<Member> memberList = memberDao.getMemberByPage(acct, 0, pageSize);
		rowData = getRowData(memberList);
		//总页数
		totalPage = (int)Math.ceil(memberDao.getMemberCount(acct)/(1.0*pageSize));
		model = new DefaultTableModel(rowData, columnNames);
		table = new JTable();
		table.setModel(model);
		//设置表格内容
		table.setFont(new Font(null,Font.BOLD,14));
		table.setRowHeight(30);
		//添加表头
		add(table.getTableHeader(),BorderLayout.NORTH);
		//添加表格内容
		add(table,BorderLayout.CENTER);
	}
	
	//根据用户类型初始化数据
	public void initData(int type) {
		columnNames = new String[]{"用户类型","用户名"};
		//获取第一页的成员信息，并转为二维数据
		List<Member> memberList = memberDao.getMemberByPage(type, 0, pageSize);
		rowData = getRowData(memberList);
		//总页数
		totalPage = (int)Math.ceil(memberDao.getMemberCount(type)/(1.0*pageSize));
		model = new DefaultTableModel(rowData, columnNames);
		table = new JTable();
		table.setModel(model);
		//设置表格内容
		table.setFont(new Font(null,Font.BOLD,14));
		table.setRowHeight(30);
		//添加表头
		add(table.getTableHeader(),BorderLayout.NORTH);
		//添加表格内容
		add(table,BorderLayout.CENTER);
	}
	
	//将成员数据list转为二维数组
	public Object[][] getRowData(List<Member> list) {
		if(list!=null && list.size()>0){
			Object[][] data = new Object[list.size()][2];
			for(int i=0;i<list.size();i++){
				if(list.get(i).getType()==0)
					data[i][0] = "管理员";
				else
					data[i][0] = "普通用户";
				data[i][1] = list.get(i).getAcct();
			}
			return data;
		}
		return null;
	}
	
	//初始化页面
	public void initPage(){
		prevBtn = new JButton("上一页");
		nextBtn = new JButton("下一页");
		label1.setText("第 ");
		label2.setText("1");
		label3.setText(" / "+totalPage+" 页");
		prevBtn.addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				if(currPage>1){
					currPage--;
					label2.setText(currPage+"");
					List<Member> list = memberDao.getMemberByPage((currPage-1)*pageSize, pageSize);
					rowData = getRowData(list);
					model = new DefaultTableModel(rowData,columnNames);
					table.setModel(model);
				}
				setBtn();	
			}
		});
		nextBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(currPage<totalPage){
					currPage++;
					label2.setText(currPage+"");
					List<Member> list = memberDao.getMemberByPage((currPage-1)*pageSize, pageSize);
					rowData = getRowData(list);
					model = new DefaultTableModel(rowData,columnNames);
					table.setModel(model);
				}
				setBtn();	
			}
		});
		panel.setBackground(Color.WHITE);
		panel.add(prevBtn);
		panel.add(label1);
		panel.add(label2);
		panel.add(label3);
		panel.add(nextBtn);
		panel.setLayout(new FlowLayout());
		setBtn();
		add(panel,BorderLayout.SOUTH);
	}
	
	//设置按钮状态
	public void setBtn(){
		if(currPage==1){
			prevBtn.setEnabled(false);
		}else{
			prevBtn.setEnabled(true);
		}
		
		if(currPage==totalPage){
			nextBtn.setEnabled(false);
		}else{
			nextBtn.setEnabled(true);
		}
	}
	
	//清空组件
	public void clear() {
		remove(table.getTableHeader());
		remove(table);
		remove(panel);
	}
	
}
